package mini.ideashare.cms.manager;

import mini.ideashare.cms.dao.TB_ItemDAO;
import mini.ideashare.cms.model.TB_Item;
import mini.ideashare.cms.model.qc.Page;
import mini.ideashare.cms.model.qc.TB_ItemQC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class TB_ItemManager {

    @Autowired
    private TB_ItemDAO tbItemDAO;

    public List<TB_Item> queryItem(TB_Item item,Integer pageIndex , Integer pageSize){
        TB_ItemQC qc  = new TB_ItemQC();
        if(pageIndex!=null && pageSize!=null){
            Page page = new Page(pageIndex,pageSize);
            qc.setPage(page);
        }
        qc.setId(item.getId()).setSkuName(item.getSkuName()).setCategoryId(item.getCategoryId());
        List<TB_Item> tb_items = tbItemDAO.listTB_Item(qc);
        return tb_items;
    }

    public Long countItem(TB_Item item){
        TB_ItemQC qc  = new TB_ItemQC();
        qc.setId(item.getId()).setSkuName(item.getSkuName());
        Long count = tbItemDAO.countTB_Item(qc);
        return count;
    }

    public int saveItem(TB_Item item){

        if(item.getId()!=null){
            return tbItemDAO.updateTB_Item(item);
        }else {
            return tbItemDAO.insertTB_Item(item);
        }

    }

    public int deleteItem(Long id){

        return tbItemDAO.deleteTB_ItemById(id);

    }



}
